package com.bill.common.system.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.bill.common.system.dao.OrderDAO;
import com.bill.common.system.dto.OrderDTO;
import com.bill.common.system.entity.Orders;
import com.bill.common.user.service.IRegisterUserService;
import com.sinba.common.dto.PageDTO;

public class OrderService implements IOrderService {
	@Autowired
	private OrderDAO orderDAO;
	@Autowired
	private IRegisterUserService registerUserService;
	
	
	@Transactional
	public PageDTO findOrderPager(OrderDTO qdto,String sord, String sidx){
		PageDTO pagedto = orderDAO.findOrderPager(qdto, sord, sidx);
		List<Orders> list = (List<Orders>)pagedto.getData();
		List<OrderDTO> orderDtos = new ArrayList<OrderDTO>();
		for(Orders entity:list){
			OrderDTO dto = new OrderDTO();
			OrderDTO.entity2dto(entity, dto);
			orderDtos.add(dto);
		}
		pagedto.setData(orderDtos);
		return pagedto;
	};
	@Transactional
	public Long saveOrder(OrderDTO dto){
		Orders entity = new Orders();
		OrderDTO.dto2entity(dto, entity);
		entity.setCreateTime(new Date());
		orderDAO.save(entity);
		return entity.getId();
	};
	@Transactional
	public void deleteOrder(Long[] ids){
		orderDAO.deleteByIds(ids);
	};
	
	@Transactional
	public OrderDTO findOrderInfo(Long orderId){
		Orders entity = orderDAO.findById(orderId);
		OrderDTO orderDTO = new OrderDTO();
		OrderDTO.entity2dto(entity , orderDTO);
		return orderDTO;
	}
	@Transactional
	public void updateOrder(String orderNo){
		Orders entity = orderDAO.findUniqueBy("orderNo", orderNo);
		if(!"已成功支付".equals(entity.getOrderStatus())){
			System.out.println("更新订单：：：："+orderNo);
			registerUserService.userRecharge("payadd", entity.getPayMoeny().doubleValue(), entity.getLoginName());	
			entity.setOrderStatus("已成功支付");
		}
		
	}
	
}
